<table id="sn" class="easyui-datagrid" title="Cetak SN" style="height:500px"
                data-options="
                        url: '<?php echo $this->baseUrl() ?>/jsondata/cetaksn',
                        fitColumns: true,
                        singleSelect: true,
                        rownumbers: true,
                        showFooter: true,
                        autoRowWidth:true,
                        pagination:true
                ">
        <thead>
                <tr>
                        <th data-options="field:'serial_number',width:80">SN</th>
                        <th data-options="field:'pin',width:120">PIN</th>
                </tr>
        </thead>
    </table>


<script>
    $(function(){
            var pager = $('#sn').datagrid('getPager');	// get the pager of datagrid
            pager.pagination({
                    buttons:[{
                            iconCls:'icon-excel',
                            handler:function(){
                                    downloadExcel();
                            }
                    }]
            });
    })

    function validateNumber(evt) {

        var charCode = (evt.which) ? evt.which : event.keyCode
        if (charCode > 31 && (charCode < 48 || charCode > 57))
            return false;
        return true;
      }
      
    function downloadExcel(){

       var num = $('#num').val();
       
       $("#sn").datagrid("loading");

        $.ajax({
        type: 'GET',
        dataType: 'text',
        url: '<?php echo $this->baseUrl() ?>/excel/cetaksn',
        data: {num: num},
        success: function(result){
                    $("#sn").datagrid ("loaded");

                    if (result == "TIMEOUT"){
                        location.href = "<?php echo $this->baseUrl() ?>/admin";
                    }else{
                        location.href = "<?php echo $this->baseUrl() ?>/excel/"+result;
                    }
             }
        });

    }

    function cek(){
        var msg = [];

        if ($('#num').val() == "") msg.push('<span>* Jumlah SN harus diisi</span>');
        if (msg.length == 0) return true;
        else{
            //alert("Data tidak lengkap:\n\n" + msg);
            //$.messager.alert("Data tidak lengkap:\n\n" + msg);
            $.messager.alert('Info', msg.join('<br/>'));
            return false;
        }
    }

    function cariSN(){

            if(cek()){

            var num = $('#num').val();


            $('#sn').datagrid({
                queryParams: {
                page: 1,
                rows: num,
                num: num
                }
                });


         var pager = $('#sn').datagrid('getPager');	// get the pager of datagrid
            pager.pagination({
                    buttons:[{
                            iconCls:'icon-excel',
                            handler:function(){
                                    downloadExcel();
                            }
                    }]
            });

        }
            
    }

   
</script>




